package com.example.demo.test;

import com.example.jpa.crud.annotation.ViewTemplate;
import com.example.jpa.crud.model.JsonViewTemplate;
import lombok.Getter;
import lombok.Setter;

@ViewTemplate
public class CodeMessageData implements JsonViewTemplate {

    // 自定义 Json 视图
    @Setter
    @Getter
    static class Model {
        private long code;
        private String message;
        private Object data;
    }

    @Override
    public Object ok(Object data, boolean update, long effect) {
        final Model model = new Model();
        if (update) {
            if (effect > 0L) {
                model.setCode(200L);
                model.setMessage("ok");
            } else {
                model.setCode(500L);
                model.setMessage("err");
            }
        } else {
            model.setCode(200L);
            model.setMessage("ok");
        }
        model.setData(data);
        return model;
    }

    @Override
    public Object err(Exception e) {
        final Model model = new Model();
        model.setCode(500L);
        model.setData(null);
        model.setMessage(e.getMessage());
        return model;
    }

}